var express = require('express');
var router = express.Router();
const User = require('../../model/user');

const jwt = require('../../dao/jwt')
const bcrypt = require('../../dao/bcrypt')

//首页
router.get('/', function(req, res, next) {
    // console.log(req)
    // res.send('1')
    //获取前端传来的token
    let token = req.query.token || req.body.token || req.headers.token;
    //校验token
    if (token) {
        let result = jwt.verifyToken(token)
        let username = result.username
        User.findOne({
            username: username
        }).then(data => {
            let userInfo = {
                _id: data._id,
                avatar: data.avatar,
                signatrue: data.signatrue,
                sex: data.sex,
                username: data.username,
                nickname: data.nickname,
                isAdmin: data.isAdmin
            }
            res.send(userInfo)
        })
    } else {
        res.send(false)
    }
    // let _id = req.query._id
});
//注册
router.post('/register', (req, res, next) => {
    let data = req.body
    console.log(req.body)
    let resData = {}
        // console.log(data);
    User.find({
        username: data.username
    }).then(userInfo => {
        if (userInfo != '') {
            console.log(userInfo)
            resData.msg = '用户名已存在'
            resData.code = 1
            res.send(resData)
        } else {
            // let psw = bcrypt.encryption(data.password)
            let user = new User({
                username: data.username,
                password: data.password,
                nickname: data.nickname
            })
            user.save()
            resData.msg = '注册成功，即将自动登录'
            resData.code = 0
            let token = jwt.generateToken(data.username)
            resData.token = token
            res.send(resData)
        }
    })
});
//登录
router.post('/login', (req, res, next) => {
    console.log('到这里1')
    let data = req.body;
    let resData = {}
    User.findOne({
        username: data.username,
        password: data.password
    }).then(userInfo => {
        // console.log(userInfo)
        if (userInfo == null) {
            resData.msg = '用户名或密码错误'
            resData.code = 1
            res.send(resData)
        } else {
            // console.log('到这里cookies');
            let token = jwt.generateToken(userInfo.username);
            // req.session.user = userInfo._id;
            // console.log(req.session);
            // res.set('Set-Cookies', 'foo=bar;Path=/;HttpOnly')
            resData.userInfo = {
                _id: userInfo._id,
                avatar: userInfo.avatar,
                signatrue: userInfo.signatrue,
                sex: userInfo.sex,
                username: userInfo.username,
                nickname: userInfo.nickname,
                isAdmin: userInfo.isAdmin
            }
            console.log(resData.userInfo)
            resData.msg = '登录成功'
            resData.code = 0
            resData.token = token
            res.json(resData)
            return
        }
    })
});
//修改信息
router.post('/edit', (req, res, next) => {
    console.log(req.body)
    let _id = req.body._id
    console.log(_id)
    User.findByIdAndUpdate({ _id }, {
        nickname: req.body.nickname,
        sex: req.body.sex,
        signatrue: req.body.signatrue,
        avatar: req.body.avatar
    }, (err, ret) => {
        if (err) {
            console.log(err)
            return
        } else {
            console.log(ret)
            res.send('1')
        }
    })

});
//获取点击用户的信息
router.get('/getuser', (req, res, next) => {
    let uid = req.query.uid
    console.log(uid)
    User.findById(uid).then(userInfo => {
        let data = {
            _id: userInfo._id,
            avatar: userInfo.avatar,
            signatrue: userInfo.signatrue,
            sex: userInfo.sex,
            username: userInfo.username,
            nickname: userInfo.nickname,
            isAdmin: userInfo.isAdmin
        }
        res.send(data)
    })
})
module.exports = router;